#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

void demonstrate_generic_lambdas() {
    // C++14泛型lambda: 参数类型自动推导
    auto print = [](auto value) {
        std::cout << "  值: " << value << '\n';
    };

    // 对不同类型使用同一lambda表达式
    print(42);         // int
    print(3.14159);    // double
    print("C++14");    // const char*
    print(std::string("Generic Lambda")); // std::string

    // 在算法中使用
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    std::cout << "  将所有元素乘以2:\n  ";
    std::for_each(numbers.begin(), numbers.end(), [](auto& n) {
        n *= 2;
        std::cout << n << " ";
    });
    std::cout << '\n';

    // 使用auto&
    auto add_to_all = [](auto& container, const auto& value) {
        for (auto& item : container) {
            item += value;
        }
    };

    add_to_all(numbers, 10);
    std::cout << "  将所有元素加10:\n  ";
    for (const auto& n : numbers) {
        std::cout << n << " ";
    }
    std::cout << '\n';

    // 使用带有状态的泛型lambda
    auto counter = [count = 0](auto) mutable {
        return ++count;
    };

    std::cout << "  带状态的lambda计数:\n";
    std::cout << "  " << counter(1) << '\n';
    std::cout << "  " << counter("hello") << '\n';
    std::cout << "  " << counter(3.14) << '\n';
} 